﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DATN.mds.ContactManagement
{
    public partial class DeleteCM : System.Web.UI.Page
    {
        string cnnString;
        SqlConnection cnn;
        SqlCommand cmd;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Us_Id"] != null && Session["Cus_Id"] != null)
                {
                    //Bind data vào GridView
                    BindData();

                    //Note Remind
                    Session["Remind"] = null;
                }
                else
                {
                    Response.Redirect("../Account/Login.aspx");
                }
                Validate();
            }
        }

        //Bind Data vào Lable
        protected void BindData()
        {
            try
            {
                Lb_Detete.Text = "";
                string Cus_Se = Session["Cus_Id"].ToString();
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                String queryString = "SELECT * FROM [dbo].[Customers]  WHERE (([Cus_Id] = @Cus_Id))";
                cmd = new SqlCommand(queryString, cnn);
                cmd.Parameters.Add("@Cus_Id", SqlDbType.Int);
                cmd.Parameters["@Cus_Id"].Value = Convert.ToInt32(Cus_Se);

                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    LbFirstName.Text = reader["FirstName"].ToString();
                    LbLastName.Text = reader["LastName"].ToString();
                    LbBirthday.Text = reader["Birthday"].ToString();
                    LbPhone.Text = reader["Phone"].ToString();
                    LbMail.Text = reader["Mail"].ToString();
                    LbAddress.Text = reader["Address"].ToString();
                    LbCompany.Text = reader["Company"].ToString();
                    LbSalary.Text = reader["Salary"].ToString();
                    LbHabit.Text = reader["Habit"].ToString();
                    LbCity.Text = reader["City"].ToString();
                    LbMarried.Text = reader["MaritalStatus"].ToString();
                    LbJob.Text = reader["Job"].ToString();
                }
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
                cnn.Close();
            }
        }

        //Click Button Xóa
        protected void Bt_Delete_Click(object sender, EventArgs e)
        {
            //Delete History
            DelHis();
            //Delete Customer
            DelCus();
            Session.Contents.Remove("Cus_Id");

            Lb_Detete.Text = "Xóa thành công";
            Response.Redirect("../mds.ContactManagement/IndexCM.aspx");
        }

        //Delete from History
        protected void DelHis()
        {
            try
            {
                int Cus_id = Convert.ToInt32(Session["Cus_Id"].ToString());
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();

                cmd = cnn.CreateCommand();
                cmd.CommandText = "DELETE FROM [dbo].[Histories] WHERE (([Cus_Id] = @Cus_Id))";
                cmd.Parameters.Add("@Cus_Id", SqlDbType.Int);
                cmd.Parameters["@Cus_Id"].Value = Cus_id;
                cmd.ExecuteNonQuery();
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }   
        }

        //Delete Customers
        protected void DelCus()
        {
            try {
                int Cus_id = Convert.ToInt32(Session["Cus_Id"].ToString());
                cnnString = ConfigurationManager.ConnectionStrings["DATNconnection"].ToString();
                cnn = new SqlConnection(cnnString);
                cnn.Open();
                cmd = cnn.CreateCommand();
            
                cmd = cnn.CreateCommand();
                cmd.CommandText = "DELETE FROM [dbo].[Customers] WHERE (([Cus_Id] = @Cus_Id))";
                cmd.Parameters.Add("@Cus_Id", SqlDbType.Int);
                cmd.Parameters["@Cus_Id"].Value = Cus_id;
                cmd.ExecuteNonQuery();

                //Response.Write("abc");
                cnn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }   
        }

        //Button Trở Về trang IndexCM
        protected void bt_Cancel_Click(object sender, EventArgs e)
        {
            Session.Contents.Remove("Cus_Id");
            Response.Redirect("../mds.ContactManagement/IndexCM.aspx");
        }
    }
}